package com.tzxs.pay.main.gather.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.util.Log;

import com.tzxs.pay.main.bill.detail.model.BankRefundBean;
import com.tzxs.pay.main.bill.detail.model.CancelAutBean;
import com.tzxs.pay.main.gather.model.BankBackBean;
import com.tzxs.pay.main.gather.model.BankBean;
import com.tzxs.pay.main.gather.resource.TradeResource;
import com.tzxs.pay.util.SingleLiveEvent;
import com.tzxs.pay.util.netnavigator.SimpleNetNavigator;
import com.tzxs.pay.util.viewmodel.Callback;

import java.util.HashMap;
import java.util.Map;

public class BankViewModel extends AndroidViewModel {

    private static final String TAG = BankViewModel.class.getSimpleName();
    public SingleLiveEvent<String> dialogText = new SingleLiveEvent<>();
    public SingleLiveEvent<String> snackText = new SingleLiveEvent<>();


    public ObservableField<BankBean.DataBean> bankBean = new ObservableField<>();
    //结算bean
    public ObservableField<BankBean.DataBean> settmentedBean = new ObservableField<>();
    //预授权撤销bean
    public ObservableField<CancelAutBean.DataBean> cancelBean = new ObservableField<>();


    //银行卡退款bean
    public ObservableField<BankRefundBean.DataBean> bankRefund = new ObservableField<>();

    public BankViewModel(@NonNull Application application) {
        super(application);

    }


    /**
     * 创建银行卡订单
     *
     * @param amount   金额
     * @param payType  类型   普通收款 预授权
     * @param remark   备注
     * @param authType 当为预授权的时候传1
     * @param orderId  订单号  当为退款操作的时候
     * @param callback 回调
     */
    public void createBankOrder(double amount, String payType, String remark,
                                int authType, String orderId,  Callback callback) {


        Log.d(TAG, "onClick: 传递的参数值: amount:" + amount +
                "\npayType:" + payType + "\nremark:" + remark + "authType:" + authType + "orderId:" + orderId);
        TradeResource.getInstance(getApplication()).createBanOrder(amount, payType, remark, authType, orderId,
                new SimpleNetNavigator<BankBean>(snackText, dialogText, callback) {
                    @Override
                    public String getTitle() {

                        return "正在创建订单...";
                    }

                    @Override
                    public void success(BankBean data) {

                        if (data.getCode() == 10001) {
                            snackText.setValue("创建订单成功!");
                            if (data.getData() != null) {
                                bankBean.set(data.getData().get(0));
                            }
                            callback.success();
                        } else {
                            snackText.setValue(data.getMsg());
                        }

                    }

                    @Override
                    public void fail(String error) {
                        snackText.setValue(error);
                    }
                });
    }

    /**
     * 创建银行卡订单
     *
     * @param amount   金额
     * @param payType  类型   普通收款 预授权
     * @param remark   备注
     * @param authType 当为预授权的时候传1
     * @param orderId  订单号  当为退款操作的时候
     * @param callback 回调
     */
    public void createBankSettmentedOrder(double amount, String payType, String remark,
                                          int authType, String orderId,Callback callback) {
        Log.d(TAG, "onClick: 传递的参数值: amount:" + amount +
                "\npayType:" + payType + "\nremark:" + remark + "authType:" + authType + "orderId:" + orderId);
        TradeResource.getInstance(getApplication()).createBanOrder(amount, payType, remark, authType, orderId,
                new SimpleNetNavigator<BankBean>(snackText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return null;
                    }

                    @Override
                    public void success(BankBean data) {
                        if (data.getCode() == 10001) {
                            if (data.getData() != null) {
                                settmentedBean.set(data.getData().get(0));
                            }
                            callback.success();
                        } else {
                            snackText.setValue(data.getMsg());
                        }

                    }

                    @Override
                    public void fail(String error) {
                        snackText.setValue(error);
                    }
                });
    }

    /**
     * 银行卡交易回调
     *
     * @param data     数据
     * @param callback 回调
     */
    public void uploadTradeInfo(String data, Callback callback) {
        TradeResource.getInstance(getApplication()).uploadTradeInfo(data, new SimpleNetNavigator<BankBackBean>(snackText, dialogText, callback) {
            @Override
            public String getTitle() {
                return null;
            }

            @Override
            public void success(BankBackBean data) {
                if (data.getCode() == 10001) {
                    callback.success();


                } else {
                    dialogText.setValue("上传信息失败!");
                }
            }

            @Override
            public void fail(String error) {
                dialogText.setValue(error);
            }
        });

    }

    //支付宝 微信 预授权撤销

    public void cancelAut(String auth_no, String password, String remark, Callback callback) {

        Log.d(TAG, "cancelAut: 预授权撤销参数:" + auth_no + "\n" + password + "\n" + remark);
        TradeResource.getInstance(getApplication()).cancelAut(auth_no, password, remark,
                new SimpleNetNavigator<CancelAutBean>(snackText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "预授权撤销中...";
                    }

                    @Override
                    public void success(CancelAutBean data) {

                        if (data.getCode() == 10001) {
                            cancelBean.set(data.getData().get(0));
                            callback.success();
                        }
                        snackText.setValue(data.getMsg());

                    }

                    @Override
                    public void fail(String error) {
                        snackText.setValue(error);
                    }
                });
    }


    //银行卡预授权撤销
    public void cancelBankAut(String orderId, Callback callback) {

        TradeResource.getInstance(getApplication()).cancleBankAut(orderId, new SimpleNetNavigator<BankBean>(snackText, dialogText, callback) {
            @Override
            public String getTitle() {
                return "正在撤销...";
            }

            @Override
            public void success(BankBean data) {
                if (data.getCode() == 10001) {
                    bankBean.set(data.getData().get(0));
                    callback.success();
                } else {
                    snackText.setValue(data.getMsg());
                }
            }

            @Override
            public void fail(String error) {

                snackText.setValue(error);
            }
        });

    }

    //银行卡退款订单创建
    public void bankRefundOrCreate(float amount, String password, String remark, String orderId, Callback callback) {

        TradeResource.getInstance(getApplication()).bankRefundOrCreate(amount, password, remark, orderId,
                new SimpleNetNavigator<BankRefundBean>(snackText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在退款...";
                    }

                    @Override
                    public void success(BankRefundBean data) {
                        if (data.getCode() == 10001) {

                            bankRefund.set(data.getData().get(0));
                            callback.success();
                        } else {
                            snackText.setValue(data.getMsg());
                        }
                    }
                });
    }


}
